<?php

header('Cache-Control: private, no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');

try {
    include_once './config.php';
    include_once './Situare.php';
    include_once './display.php';
    include_once './client_action_handler.php';
    include_once './Utils.php';
    include_once './log4php/Logger.php';

    Logger::configure('./log4j.properties');

    $log = Logger::getLogger(__FILE__);

    Utils::logPhpGlobals($log);

    // check the browser
    include 'clientChecker.php';

    // session is OK
    // handle the required actions according to the request parameters
    include 'action_handler.php';
    // create Situare instance after the actions done by the action_handler.php
    /**
     * @var Situare
     */
    $situare = new Situare();

    // in case of invalid session redirect to the FB login page
    if (!$situare->isValidSession()) {
        $url = $situare->getMobilWebLoginURL();
        $log->debug("Not found valid session! Redirection to: " . $url);
        header("Location:" . $url);
        exit();
    }

    // NOTE: renderMainHeader() renders also the CSS file required by renderTitleBar()
    echo Display::renderHTMLPage($situare, I18n::getMessage('app.title'), Display::renderMainHeader($situare), Display::renderMainBody($situare) . handleAction($situare));
} catch (Exception $e) {
    $log->error("Uncaught expection:");
    $log->error("Code: " . $e->getCode());
    $log->error("Message: " . $e->getMessage());
    $log->error("Location: " . $e->getFile() . " at line " . $e->getLine());
    $log->error("Trace: " . $e->getTraceAsString());

    // TODO return complette valid XHTML page
    echo "\n<br/><b>Uncaught expection:</b> ";
    echo "\n<br/><b>Code:</b> " . $e->getCode();
    echo "\n<br/><b>Message:</b> " . $e->getMessage();
    echo "\n<br/><b>Location:</b> " . $e->getFile() . " at line " . $e->getLine();
    echo "\n<br/><b>Trace:</b> " . $e->getTraceAsString();

    echo "\n<br/><br/><input type='submit' onclick='window.location=\"" . HOME_URL . "\"' value='Login'/>";
    exit();
}

?>